package com.keqiang.chart.impl.axis.impl.bar

import android.graphics.Canvas
import androidx.annotation.FloatRange
import androidx.annotation.Px
import com.keqiang.chart.base.BaseChartView
import com.keqiang.chart.impl.axis.AxisEntity
import com.keqiang.chart.impl.axis.BaseAxisChart
import com.keqiang.chart.impl.axis.BaseTouchHandler
import com.keqiang.chart.impl.axis.BaseXAxis
import com.keqiang.chart.impl.axis.XAxis
import com.keqiang.chart.impl.axis.YAxis
import com.keqiang.chart.impl.axis.impl.line.AddedLine
import com.keqiang.chart.impl.axis.render.BaseDataRender
import com.keqiang.chart.impl.axis.render.XAxisRender
import com.keqiang.chart.impl.axis.render.YAxisRender
import com.keqiang.chart.utils.px

/**
 * 柱状图
 *
 * @author Created by wanggaowan on 2022/7/29 15:15
 */
open class BarChart(view: BaseChartView<*>) : BaseAxisChart<BarData>(view) {
    override var xAxis: XAxis = XAxis()
    override var yAxis: YAxis = YAxis()
    override var yAxisRender: YAxisRender<BarChart> = YAxisRender(this)
    override var xAxisRender: XAxisRender<BarChart> = XAxisRender(this, false)
    override var dataRender: BaseDataRender<BarChart> = BarChartDataRender(this)
    override var touchHandler: BaseTouchHandler<BarChart, List<BarEntity>>? = BarTouchHandler(this)

    var highLightRender: HighLightRender? = null

    override fun getGridWidth(xAxis: BaseXAxis, xAxisLength: Float): Float {
        return xAxisLength / xAxis.labelCount
    }

    override fun isEmptyData(): Boolean {
        return data == null
    }
}

/**
 * 高亮数据绘制器
 *
 * @author Created by wanggaowan on 2022/11/1 10:07
 */
abstract class HighLightRender(val chart: BarChart) {
    /**
     * 绘制高亮内容，此时[AxisEntity.drawRectF]已就绪
     *
     * @param canvas 画布
     * @param start  水平可绘制区域起始点坐标 = x轴起始点 + [XAxis.startPadding]
     * @param top    可绘制区域顶部坐标 = y轴顶部 - [YAxis.endPadding]
     * @param end    水平可绘制区域结束点坐标 = x轴结束点 - [XAxis.endPadding]
     * @param bottom 可绘制区域底部坐标 = y轴底部 + [YAxis.startPadding]
     */
    abstract fun onDraw(canvas: Canvas, start: Float, top: Float, end: Float, bottom: Float, data: List<BarEntity>)
}

/**
 * 柱状图数据
 */
open class BarData {
    var groups: List<BarGroup>? = null

    /**
     * 附加线数据
     */
    var addedLines: List<AddedLine>? = null
}

/**
 * 柱状图分组
 */
open class BarGroup(
    /**
     * 柱状图数据
     */
    var values: List<BarEntity>) {

    /**
     * 多个柱形图之间的间隔
     */
    @Px
    @FloatRange(from = 0.0)
    var barOffset = 10f.px

    /**
     * 柱状图绘制方向，参考[GRAVITY_START]、[GRAVITY_CENTER]、[GRAVITY_END]
     */
    var gravity: Int = GRAVITY_CENTER

    companion object {
        /**
         * 从网格起始位置往结束位置绘制
         */
        const val GRAVITY_START = 0

        /**
         * 从网格中间向两边绘制
         */
        const val GRAVITY_CENTER = 1

        /**
         * 从网格结束位置往起始位置绘制
         */
        const val GRAVITY_END = 2
    }
}